import tkinter as tk
from tkinter import *
import tkinter.messagebox
import re
import requests
import json
import sys
import os
import time

code=input('请输入股票代码,如sz000001或sh000001：\n')
url='http://hq.sinajs.cn/list='
url=url+code

headers = {'content-type': 'application/json',
                   'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0'}
r = requests.get(url, headers=headers)
# print(r.text)
name_format=r'[\u4e00-\u9fa5]+'
data_all_format=r'\d+\.\d+'
# 匹配股票代码
stock_code_format=r'\d+'
stock_code=re.findall(stock_code_format,r.text)
# 匹配股票名称
stock_name=re.findall(name_format,r.text)
# 匹配具体数据
data_all=re.findall(data_all_format,r.text)

# print(''.join(stock_name))
# print(data_all)

# 股票名字,今日开盘价,昨日收盘价,当前价格,今日最高价,今日最低价,竞买价,竞卖价,成交股票数,成交金额,
print('股票代号',stock_code[0])
print('股票名称',''.join(stock_name))
stock_data=data_all[0]
today_open_price=data_all[0]
print('今日开盘价',today_open_price)
yesterday_close_price=data_all[1]
print('昨日收盘价',yesterday_close_price)
today_price=data_all[2]
print('当前价格',today_price)
today_Max=data_all[3]
print('今日最高',today_Max)
today_Min=data_all[4]
print('今日最低',today_Min)
ontime_rate=(float(today_price)-float(yesterday_close_price))/float(yesterday_close_price)*100
print('实时价格 %.2f'% ontime_rate,'%')